package org.ielse.frame.test;

import org.ielse.frame.BaseActivity;
import org.ielse.frame.R;
import org.ielse.frame.t.volley.VolleyImageUtil;
import org.ielse.frame.view.ImageView;
import org.ielse.frame.view.SwitchView;
import org.ielse.frame.view.TitleLayout;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;

public class NormalActivity extends BaseActivity {

	private TitleLayout layTitle;
	private SwitchView vsJustWifi;

	private String testImageUrl1 = "http://my.csdn.net/uploads/avatar/5/1/A/1_bfbx5173.jpg";
	private String testImageUrl2 = "http://a.hiphotos.baidu.com/news/q%3D100/sign=662de0a55643fbf2c32ca223807fca1e/b58f8c5494eef01f9a58dd68e5fe9925bd317dcb.jpg";

	@Override protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_normal);
		super.onCreate(savedInstanceState);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

		layTitle = (TitleLayout) findViewById(R.id.lay_title);
		layTitle.pushContent(null, getString(R.string.action_settings), null, false);

		final ImageView imgTest = (ImageView) findViewById(R.id.img_test);
		imgTest.setType(ImageView.TYPE_ROUND);
		imgTest.loadImage(testImageUrl1);

		imgTest.setOnClickListener(new View.OnClickListener() {
			@Override public void onClick(View v) {
				layPicture.showPicture(testImageUrl1, v);
			}
		});

		final ImageView imgTest2 = (ImageView) findViewById(R.id.img_test2);
		imgTest2.setType(ImageView.TYPE_OVAL);
		imgTest2.loadImage(testImageUrl2);
		imgTest2.setOnClickListener(new View.OnClickListener() {
			@Override public void onClick(View v) {
				layPicture.showPicture(testImageUrl2, v);
			}
		});

		vsJustWifi = (SwitchView) findViewById(R.id.vs_just_wifi);
		boolean isJustWifi = appSettings.isDownloadImageJustWifi();
		vsJustWifi.setState(isJustWifi);
		vsJustWifi.setOnStateChangedListener(new SwitchView.OnStateChangedListener() {
			@Override public void toggleToOn() {
				vsJustWifi.toggleSwitch(true);
				appSettings.setDownloadImageJustWifi(true);
				appSettings.update(appSettings.getId());
			}

			@Override public void toggleToOff() {
				vsJustWifi.toggleSwitch(false);
				appSettings.setDownloadImageJustWifi(false);
				appSettings.update(appSettings.getId());
			}
		});

		findViewById(R.id.tx_click_to_c).setOnClickListener(new View.OnClickListener() {
			@Override public void onClick(View v) {
				startActivity(RecyclerActivity.class);
			}
		});

		findViewById(R.id.btn_reset_title).setOnClickListener(new View.OnClickListener() {
			@Override public void onClick(View arg0) {
				tmpTitleIndex++;
				layTitle.pushNewContent(null, getString(R.string.confirm) + tmpTitleIndex, null, true);

			}
		});

		findViewById(R.id.btn_pop_title).setOnClickListener(new View.OnClickListener() {
			@Override public void onClick(View arg0) {
				layTitle.popToLastContent();
			}
		});

		findViewById(R.id.btn_query_image_from_camera).setOnClickListener(new View.OnClickListener() {
			@Override public void onClick(View v) {
				openCamera();
			}
		});

		findViewById(R.id.btn_query_image_from_photo).setOnClickListener(new View.OnClickListener() {
			@Override public void onClick(View v) {
				openPhotos();
			}
		});

		imageTmp = (android.widget.ImageView) findViewById(R.id.img_tmp);
		imageTmp.setOnClickListener(new OnClickListener() {
			@Override public void onClick(View v) {
				if (!TextUtils.isEmpty(photoPath)) layPicture.showPicture(photoPath, v);
			}
		});

	}

	private android.widget.ImageView imageTmp;
	private String photoPath = "";
	private int tmpTitleIndex;

	@Override public void onViewCallback(int action, Object... args) {
		switch (action) {
		case ACTION_QUERY_IMAGE:
			photoPath = (String) args[0];
			VolleyImageUtil.asyncGetScaledBitmap((Activity) context, photoPath, imageTmp.getWidth(), imageTmp.getHeight(),
					new VolleyImageUtil.Listener() {
						@Override public void onLoadCompleted(Bitmap bitmap) {
							imageTmp.setImageBitmap(bitmap);
						}
					});
			break;
		}

	}

}
